﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WoWGoldWatch
{
    public class GoldFileLoader
    {
        public GoldFileLoader(string directory)
        {
            Dir = new DirectoryInfo(directory);
        }

        public DirectoryInfo Dir
        {
            get;
            private set;
        }

        public List<GoldFile> GoldFiles
        {
            get
            {
                List<GoldFile> goldFiles = new List<GoldFile>();
                foreach (FileInfo fi in Dir.GetFiles("*.gold.txt"))
                {
                    goldFiles.Add(ReadGoldFile(fi.FullName));
                }

                return goldFiles;
            }
        }

        GoldFile ReadGoldFile(string fn)
        {
            List<GoldFileEntry> entries = new List<GoldFileEntry>();
            foreach (string line in File.ReadAllLines(fn))
            {
                if (line == "")
                {
                    continue;
                }

                string[] split = line.Split(new string[] { " > " }, StringSplitOptions.RemoveEmptyEntries);

                GoldFileEntry entry = new GoldFileEntry();
                entry.Date = DateTime.Parse(split[0]);
                entry.Gold = Convert.ToInt32(Double.Parse(split[1]) / 10000);

                entries.Add(entry);
            }

            GoldFileEntry lastEntry = entries[entries.Count - 1];

            GoldFile f = new GoldFile();
            f.Entries.AddRange(entries);
            f.ServerName = Path.GetFileName(fn);
            f.ServerName = f.ServerName.Substring(0, f.ServerName.IndexOf("."));
            f.Gold = lastEntry.Gold;
            f.LastUpdate = lastEntry.Date;

            return f;
        }
    }

    public class GoldFile
    {
        public GoldFile()
        {
            Entries = new List<GoldFileEntry>();
        }

        public int Gold
        {
            get;
            set;
        }

        public string ServerName
        {
            get;
            set;
        }

        public DateTime LastUpdate
        {
            get;
            set;
        }

        public List<GoldFileEntry> Entries
        {
            get;
            private set;
        }
    }

    public class GoldFileEntry
    {
        public DateTime Date
        {
            get;
            set;
        }

        public int Gold
        {
            get;
            set;
        }
    }
}
